#!/bin/bash

#获取内网ip
LOCAL_AREA_NETWORK=$(ifconfig eth0 | grep inet |grep netmask|awk '{print $2}')
#获取外网ip
PUBLIC_NET=$(ifconfig eth1 | grep inet |grep netmask|awk '{print $2}')
# 获取外网ip后缀
SUFFIX=${PUBLIC_NET##*.}
# 设置容器名字
CONTAINER_NAME=kafka_${SUFFIX}

ZOOKEEPER_IP="112.74.114.59"


# 删除容器
if docker ps -a | grep $CONTAINER_NAME | awk '{print $1 }' ; then
    docker rm -f $CONTAINER_NAME
fi



docker run -d --privileged\
    --name $CONTAINER_NAME \
    -v ${PWD}/data:/data \
    -v ${PWD}/logs:/logs \
    -p 9092:9092 -p 7203:7203 \
    --env KAFKA_ADVERTISED_HOST_NAME=$LOCAL_AREA_NETWORK \
    --env ZOOKEEPER_IP=$ZOOKEEPER_IP \
    --env KAFKA_DELETE_TOPIC_ENABLE=true \
    --env KAFKA_BROKER_ID=$SUFFIX \
    ches/kafka

# logs
docker logs -f $CONTAINER_NAME